#include "pokemon.h"
#include <algorithm>

// 元素相克计算实现
float getElementEffectiveness(Element attacker, Element defender) {
    switch (attacker) {
    case Element::Water: return (defender == Element::Fire || defender == Element::Steel) ? 1.5f : 1.0f;
    case Element::Grass: return (defender == Element::Water || defender == Element::Rock) ? 1.5f : 1.0f;
    case Element::Fire: return (defender == Element::Grass || defender == Element::Steel) ? 1.5f : 1.0f;
    case Element::Steel: return (defender == Element::Grass || defender == Element::Rock) ? 1.5f : 1.0f;
    case Element::Rock: return (defender == Element::Fire || defender == Element::Water) ? 1.5f : 1.0f;
    default: return 1.0f;
    }
}

// Skill类实现
Skill::Skill(QString name, int power, Element element)
    : m_name(name), m_power(power), m_element(element) {}

int Skill::calculateDamage(Pokemon* user, Pokemon* target) {
    float modifier = getElementEffectiveness(m_element, target->getElement());
    return std::max(1, static_cast<int>(m_power * modifier));
}

void Skill::use(Pokemon* user, Pokemon* target) {
    int damage = calculateDamage(user, target);
    target->takeDamage(damage);
    emit skillUsed(QString("%1使用了%2，造成%3点伤害！")
                       .arg(user->getName()).arg(m_name).arg(damage));
}





// Pokemon类实现
Pokemon::Pokemon(QString name, int hp, int attack, int defense, Element element, QString imagePath)
    : m_name(name), m_hp(hp), m_maxHP(hp), m_attack(attack), m_defense(defense),
    m_element(element), m_image(imagePath) {}

void Pokemon::addSkill(Skill* skill) {
    if (m_skills.size() < 4)
        m_skills.append(skill);
}

void Pokemon::takeDamage(int damage) {
    m_hp = std::max(0, m_hp - damage);
    emit hpChanged(m_hp, m_maxHP);
    if (m_hp <= 0)
        emit fainted();
}

void Pokemon::useSkill(int index, Pokemon* target) {
    if (index < m_skills.size())
        m_skills[index]->use(this, target);
}
